//
//  BselegetMoneyJLViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/23.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BselegetMoneyJLViewController.h"
#import "BgetModeyTableViewCell.h"
#import "BgetmoneyModel.h"
#import "SeleAddressView.h"
#import "PGDatePickManager.h"
#import "DISBproContentViewController.h"
@interface BselegetMoneyJLViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;
@property(nonatomic,strong)UIButton *seletimeBut;
@property(nonatomic,strong)UILabel *leftseletimeLB;//选择时间背景lable
@property(nonatomic,strong)UILabel *leftseletimeUPLB;//选择时间背景lable
@property(nonatomic,strong)UILabel *leftseletimedowLB;//选择时间背景lable
@property(nonatomic,strong)UILabel *moneyLB;//
@property(nonatomic,strong)NSString *area;
@property(nonatomic,strong)UIButton *lefttimeLB;
@property(nonatomic,strong)UIButton *righttimeLB;
@property(nonatomic,strong)UIView *bgview;
@property(nonatomic,assign)BOOL isleft;
@property(nonatomic,strong)UILabel *leftLB;
@property(nonatomic,strong)UILabel *rightLB;

@end

@implementation BselegetMoneyJLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isleft = YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"收入记录";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    [self request];
    [self showheader];
    [self showbgview];
    // Do any additional setup after loading the view.
}

-(void)showbgview{
    
    self.bgview = [[UIView alloc]initWithFrame:CGRectMake(0, kNaviHeight+45, KWIDTH, KHEIGHT)];
    self.bgview.backgroundColor = RGBA(1, 1, 1, 0.5);
    
    UIView *whithview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 210)];
    whithview.backgroundColor = [UIColor whiteColor];
    [self.bgview  addSubview:whithview];
    
    UIView *uobg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 41)];
    uobg.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
    [whithview  addSubview:uobg];
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackground = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    //    datePicker.datePickerType = PGDatePickerTypeVertical;
    datePicker.isHiddenMiddleText = false;
    datePicker.textColorOfSelectedRow = [UIColor colorWithHexString:@"#333333"];
    datePicker.lineBackgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    
    datePicker.maximumDate = [NSDate date];
    datePicker.autoSelected = true;
    datePicker.datePickerMode = PGDatePickerModeDate;
    [whithview addSubview:datePickManager.view];
    datePickManager.view.frame = CGRectMake(0, 70, KWIDTH, KHEIGHT);
    [self addChildViewController:datePickManager];
    
    UIButton *fifishBUt = [UIButton  buttonWithType:(UIButtonTypeCustom)];
    [fifishBUt setTitle:@"完成" forState:(UIControlStateNormal)];
    [whithview addSubview:fifishBUt];
    [fifishBUt setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [fifishBUt addTarget:self action:@selector(finishAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [fifishBUt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.right.offset(-16);
        make.width.offset(40);
        make.height.offset(22);
    }];
    
    UIButton *quxiaoBUt = [UIButton  buttonWithType:(UIButtonTypeCustom)];
    [quxiaoBUt setTitle:@"取消" forState:(UIControlStateNormal)];
    [whithview addSubview:quxiaoBUt];
    [quxiaoBUt setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [quxiaoBUt addTarget:self action:@selector(quxiaoAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [quxiaoBUt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.offset(16);
        make.width.offset(40);
        make.height.offset(22);
    }];
    
    self.lefttimeLB = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.lefttimeLB.frame =CGRectMake(0, 52, KWIDTH/2, 34 );
    [self.lefttimeLB setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [self.lefttimeLB setTitle: [[self getCurrentTimes] substringToIndex:10] forState:(UIControlStateNormal)];
    [whithview  addSubview:self.lefttimeLB];
    [self.lefttimeLB addTarget:self action:@selector(leftActionsss) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.righttimeLB = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.righttimeLB.frame =CGRectMake(KWIDTH/2, 52, KWIDTH/2, 34 );
    [self.righttimeLB setTitleColor:[UIColor colorWithHexString:@"#FF666666"] forState:(UIControlStateNormal)];
    NSString *yuestr  =[[[self getCurrentTimes] substringToIndex:7] substringFromIndex:5];
    NSInteger ind  = [yuestr integerValue];
    yuestr = [NSString stringWithFormat:@"%ld",ind];
    NSString *ristr  =[[[self getCurrentTimes] substringToIndex:10] substringFromIndex:8];
    NSInteger indri  = [ristr integerValue];
    ristr =  [NSString stringWithFormat:@"%ld",indri];
    
    NSString *sternian =  [[self getCurrentTimes] substringToIndex:4];
    NSString *newstr = [NSString stringWithFormat:@"%@-%@-%@",sternian,yuestr,ristr];
    
    [self.righttimeLB setTitle:newstr forState:(UIControlStateNormal)];
    [whithview  addSubview:self.righttimeLB];
    [self.righttimeLB addTarget:self action:@selector(righttimeLBActionsss) forControlEvents:(UIControlEventTouchUpInside)];
    
    UILabel *zhi = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH/2-8, 52, 16, 34)];
    zhi.textAlignment = NSTextAlignmentCenter;
    [whithview  addSubview:zhi];
    zhi.text = @"至";
    zhi.font = FontSize(14);
    
    kWeakSelf;
    self.leftLB = [[UILabel alloc]init];
    self.leftLB.backgroundColor = zhutiColor;
    [whithview addSubview:self.leftLB];
    [_leftLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.lefttimeLB.mas_bottom).offset(8);
        make.width.offset(130);
        make.height.offset(1);
        
        make.centerX.mas_equalTo(weakSelf.lefttimeLB.mas_centerX).offset(0);
    }];
    self.rightLB = [[UILabel alloc]init];
    self.rightLB.backgroundColor = [UIColor colorWithHexString:@"#FF999999"];
    [whithview addSubview:self.rightLB];
    [_rightLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.righttimeLB.mas_bottom).offset(8);
        make.width.offset(130);
        make.height.offset(1);
        make.centerX.mas_equalTo(weakSelf.righttimeLB.mas_centerX).offset(0);
    }];
}

-(void)showheader{
    UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 45)];
    head.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    UILabel *contertLb = [[UILabel alloc]init];
    contertLb.textAlignment = NSTextAlignmentCenter;
    contertLb.textColor = [UIColor colorWithHexString:@"#F6A533"];
    
    contertLb.font = FontSize(16);
    contertLb.text = [NSString stringWithFormat:@"%@(元)",_moneyStr];
    [head addSubview:contertLb];
    [contertLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.right.offset(-16);
        make.height.offset(24);
    }];
    self.moneyLB=contertLb;
    
    self.leftseletimeLB = [[UILabel alloc]init];
    self.leftseletimeLB.layer.masksToBounds = YES;
    self.leftseletimeLB.layer.cornerRadius = 13;
    self.leftseletimeLB.backgroundColor =[UIColor whiteColor];
    self.leftseletimeLB.frame = CGRectMake(10, 10, 70, 26);
    [head addSubview:self.leftseletimeLB];
    self.leftseletimeLB.userInteractionEnabled = YES;
    
    self.leftseletimeUPLB = [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 60, 14)];
    self.leftseletimeUPLB.textAlignment = NSTextAlignmentCenter;
    self.leftseletimeUPLB.font = FontSize(10);
    self.leftseletimeUPLB.textColor = zhutiColor;
    [self.leftseletimeLB addSubview:self.leftseletimeUPLB];
    self.leftseletimedowLB = [[UILabel alloc]initWithFrame:CGRectMake(10, 17, 60, 14)];
    self.leftseletimedowLB.textAlignment = NSTextAlignmentCenter;
    self.leftseletimedowLB.font = FontSize(10);
    self.leftseletimedowLB.textColor = zhutiColor;
    [self.leftseletimeLB addSubview:self.leftseletimedowLB];
    
    self.seletimeBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.seletimeBut.layer.masksToBounds = YES;
    self.seletimeBut.layer.cornerRadius = 13;
    self.seletimeBut.titleLabel.font = FontSize(13);
    //    [self.seletimeBut setBackgroundColor:[UIColor whiteColor]];
    [self.seletimeBut setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:(UIControlStateNormal)];
    [self.seletimeBut setTitle:@"今日" forState:(UIControlStateNormal)];
    [self.seletimeBut setImage:imgname(@"xiangxiaImg") forState:(UIControlStateNormal)];
    [ self.seletimeBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.seletimeBut.imageView.frame.size.width-10, 0,  self.seletimeBut.imageView.frame.size.width)];
    [self.seletimeBut setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, - 35)];
    [ self.leftseletimeLB addSubview:self.seletimeBut];
    self.seletimeBut.frame = self.leftseletimeLB.bounds;
    self.myTableView.tableHeaderView = head;
    
    [self.seletimeBut addTarget:self action:@selector(seleTimAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)seleTimAction:(UIButton *)but{
    [self.view addSubview:_bgview];
}

-(float)widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height{CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(CGFLOAT_MAX, height) lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return sizeToFit.width;
}

-(void)request{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"type"] = _typeSYstr;
    NSString *begTime = _leftseletimeUPLB.text;
    NSString *endTime = _leftseletimedowLB.text;
    
    if (!strIsEmpty(begTime)) {
        
        NSString *ster = [[begTime substringFromIndex:5] substringToIndex:2];
        NSInteger ind = [ster integerValue];
        if (ind < 10) {
            NSMutableString* str1=[[NSMutableString alloc]initWithString:begTime];//存在堆区，可变字符串
            [str1 insertString:@"0"atIndex:5];//把一个字符串插入另一个字符串中的某一个位置
            KMyLog(@"dsfdsfds%@",str1);
            begTime = str1;
        }
        
        if (begTime.length == 9) {
            NSMutableString* ind2=[[NSMutableString alloc]initWithString:begTime];//存在堆区，可变字符串
            [ind2 insertString:@"0"atIndex:8];//把一个字符串插入另一个字符串中的某一个位置
            KMyLog(@"dsfdsfds%@",ind2);
            begTime = ind2;
        }
    }
    
    if (!strIsEmpty(endTime)) {
        
        NSString *ster = [[endTime substringFromIndex:5] substringToIndex:2];
        NSInteger ind = [ster integerValue];
        if (ind < 10) {
            NSMutableString* str1=[[NSMutableString alloc]initWithString:endTime];//存在堆区，可变字符串
            [str1 insertString:@"0"atIndex:5];//把一个字符串插入另一个字符串中的某一个位置
            KMyLog(@"dsfdsfds%@",str1);
            endTime = str1;
        }
        
        if (endTime.length == 9) {
            NSMutableString* ind2=[[NSMutableString alloc]initWithString:endTime];//存在堆区，可变字符串
            [ind2 insertString:@"0"atIndex:8];//把一个字符串插入另一个字符串中的某一个位置
            KMyLog(@"dsfdsfds%@",ind2);
            endTime = ind2;
        }
    }
    param[@"begTime"] = begTime;
    param[@"endTime"] = endTime;
    
    [NetWorkTool POST:BeMyShopassetsList param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        //        self.moneyLB.text = [NSString stringWithFormat:@"%@(元)", [[dic objectForKey:@"data"] objectForKey:@"countMoney"]];
        CGFloat ffff = 0;
        for (NSMutableDictionary *mudic in [dic objectForKey:@"data"]) {
            BgetmoneyModel *model = [[BgetmoneyModel alloc]init];
            [model setValuesForKeysWithDictionary:mudic];
            [self.mydateSource addObject:model];
            ffff = ffff+model.count_money;
        }
        self.moneyLB.text = [NSString stringWithFormat:@"(%.2f)元",ffff];
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 84;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BgetModeyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BgetModeyTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BgetmoneyModel *model = _mydateSource[indexPath.section];
    [cell refashwithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    DISASYListModel *model = _mydateSource[indexPath.section];
    //    DISBproContentViewController *VC= [[DISBproContentViewController alloc]init];
    //    VC.urls = model.Id;
    //    [self.navigationController pushViewController:VC animated:YES];
}

-(UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //_myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //    [_myTableView.mj_header beginRefreshing];
        //}];
        [_myTableView registerNib:[UINib nibWithNibName:@"BgetModeyTableViewCell" bundle:nil] forCellReuseIdentifier:@"BgetModeyTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

-(NSString*)getCurrentTimes {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    NSLog(@"currentTimeString =  %@",currentTimeString);
    return currentTimeString;
}

-(void)quxiaoAction:(UIButton *)but{
    [self.bgview removeFromSuperview];
}

-(void)finishAction:(UIButton *)buf{
    [self.bgview removeFromSuperview];
    
    [self.seletimeBut setTitleColor:[UIColor clearColor] forState:(UIControlStateNormal)];
    self.leftseletimeUPLB.text = self.lefttimeLB.titleLabel.text;
    self.leftseletimedowLB.text = self.righttimeLB.titleLabel.text;
    [self.seletimeBut setImageEdgeInsets:UIEdgeInsetsMake(3, 32, 0, - 60)];
    self.seletimeBut.width = 87;
    self.leftseletimeLB.frame = CGRectMake(10, 6, 87, 33);
    
    [self request];
}

#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    NSString *time = [NSString stringWithFormat:@"%ld-%ld-%ld",dateComponents.year,dateComponents.month,dateComponents.day];
    
    if (self.isleft) {
        [self.lefttimeLB setTitle:time forState:(UIControlStateNormal)];
    } else {
        [self.righttimeLB setTitle:time forState:(UIControlStateNormal)];
    }
}

-(void)leftActionsss{
    self.leftLB.backgroundColor = zhutiColor;
    
    [self.lefttimeLB setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    
    self.rightLB.backgroundColor = [UIColor colorWithHexString:@"#FF999999"];
    [self.righttimeLB setTitleColor:[UIColor colorWithHexString:@"#FF666666"] forState:(UIControlStateNormal)];
    self.isleft = YES;
}

-(void)righttimeLBActionsss{
    self.isleft = NO;
    self.rightLB.backgroundColor = zhutiColor;
    [self.righttimeLB setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    self.leftLB.backgroundColor = [UIColor colorWithHexString:@"#FF999999"];
    [self.lefttimeLB setTitleColor:[UIColor colorWithHexString:@"#FF666666"] forState:(UIControlStateNormal)];
}

@end
